package com.seata.demo.controller;

import io.seata.core.context.RootContext;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.seata.demo.service.StorageService;
import com.seata.demo.common.dto.CommodityDTO;
import com.seata.demo.common.res.Resp;
import lombok.extern.slf4j.Slf4j;

/**
* @comments
* @author  zg
* @date 2021年2月9日
*/
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/storage/rest-service/")
public class StorageRestServiceController {
    
    private StorageService storageService;

    /**
     * 扣减库存
     * @param commodityCode
     * @param deductCount
     * @param tx_xid
     * @return
     */
    @PostMapping("deduct-inventory")
    public Resp<CommodityDTO> deductInventory(@RequestParam String commodityCode, @RequestParam int deductCount, @RequestHeader(required = false) String tx_xid) {
        log.info("扣减库存[{}]：commodityCode={}, deductCount={}", tx_xid, commodityCode, deductCount);
        /*Resp<CommodityDTO> resp = null;
        try {
            resp = storageService.deduct(commodityCode, deductCount);
        } catch (Exception e) {
            resp = Resp.error(e.getMessage());
            log.error("", e);
        }
        return resp;*/
        return storageService.deduct(commodityCode, deductCount);
    }
    
    /**
     * 查询商品信息
     * @author zg
     * @date 2021年2月9日 
     * @param commodityCode
     * @return
     */
    /*@GetMapping("commodity")
    public Resp<CommodityDTO> selectCommodity(String commodityCode){
        
        return storageService.selectCommodity(commodityCode);
    }*/
 
}
